import Vue from 'vue'
import VueRouter from 'vue-router'

import { routes } from './routers.js'

Vue.use(VueRouter)

// VueRouter.prototype.myPush = function(location) {
//   this.push(location, () => { })
// }

const router = new VueRouter({
  routes,
  // 滚动行为
  // behavior: 'smooth  设置滚动动画'
  scrollBehavior(to, from, savedPosition) {
    // 如果是回退行为就返回之前的位置
    if (savedPosition) return savedPosition
    return { x: 0, y: 0, behavior: 'smooth' }
  }
}
)

// 重写路由push()
const vPush = router.push

VueRouter.prototype.push = function(location) {
  vPush.call(this, location, () => { })
}

export default router
